<template>
  <div class="dashboard-editor-container">
    <github-corner class="github-corner"/>

    <panel-group @handleSetLineChartData="handleSetLineChartData"/>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <line-chart :chart-data="lineChartData"/>
    </el-row>

    <el-row :gutter="32" v-show="false">
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <raddar-chart/>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart/>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <bar-chart/>
        </div>
      </el-col>
    </el-row>

    <!--<el-row :gutter="8">
      <el-col :xs="{span: 24}" :sm="{span: 24}" :md="{span: 24}" :lg="{span: 12}" :xl="{span: 12}" style="padding-right:8px;margin-bottom:30px;">
        <transaction-table />
      </el-col>
      <el-col :xs="{span: 24}" :sm="{span: 12}" :md="{span: 12}" :lg="{span: 6}" :xl="{span: 6}" style="margin-bottom:30px;">
        <todo-list />
      </el-col>
      <el-col :xs="{span: 24}" :sm="{span: 12}" :md="{span: 12}" :lg="{span: 6}" :xl="{span: 6}" style="margin-bottom:30px;">
        <box-card />
      </el-col>
    </el-row>-->
  </div>
</template>

<script>
  import GithubCorner from '@/components/GithubCorner'
  import PanelGroup from './components/PanelGroup'
  import LineChart from './components/LineChart'
  import RaddarChart from './components/RaddarChart'
  import PieChart from './components/PieChart'
  import BarChart from './components/BarChart'
  import TransactionTable from './components/TransactionTable'
  import TodoList from './components/TodoList'
  import BoxCard from './components/BoxCard'

  let lineChartData = {
    patients: {
    },
    appointments: {
    },
    records: {
    },
    pays: {
    }
  }

  import {getStatisticsDetail} from "@/api/statistics"
  import {getLastWeek} from "@/utils/date";

  export default {
    name: 'DashboardAdmin',
    components: {
      GithubCorner,
      PanelGroup,
      LineChart,
      RaddarChart,
      PieChart,
      BarChart,
      TransactionTable,
      TodoList,
      BoxCard
    },
    data() {
      return {
        appointments: {
          "预约成功待支付": [0, 0, 0, 0, 0, 0, 0],
          "预约成功已支付": [0, 0, 0, 0, 0, 0, 0],
          "申请取消预约": [0, 0, 0, 0, 0, 0, 0],
          "取消预约完成退款": [0, 0, 0, 0, 0, 0, 0],
          "取消预约无退款需要": [0, 0, 0, 0, 0, 0, 0],
          "预约接诊": [0, 0, 0, 0, 0, 0, 0],
          "接诊完成": [0, 0, 0, 0, 0, 0, 0],
          "超时失效": [0, 0, 0, 0, 0, 0, 0]
        },
        patients: {
          "新增会员": [0, 0, 0, 0, 0, 0, 0]
        },
        pays: {
          "预支付申请": [0, 0, 0, 0, 0, 0, 0],
          "完成支付": [0, 0, 0, 0, 0, 0, 0],
          "第三方订单待支付": [0, 0, 0, 0, 0, 0, 0],
          "取消": [0, 0, 0, 0, 0, 0, 0],
          "接诊完成": [0, 0, 0, 0, 0, 0, 0],
          "超时失效": [0, 0, 0, 0, 0, 0, 0]
        },
        records: {
          "急诊完成": [0, 0, 0, 0, 0, 0, 0],
          "急诊诊中": [0, 0, 0, 0, 0, 0, 0],
          "门诊完成": [0, 0, 0, 0, 0, 0, 0],
          "门诊诊中": [0, 0, 0, 0, 0, 0, 0]
        },

        lastWeek: [],
        dataList: {},
        lineChartData: lineChartData.patients
      }
    },
    created() {
      window.app = this;
      this.lastWeek = getLastWeek()
      this.initData();
    },
    activated() {
      //进入页面刷新数据
      this.initData();
    },
    methods: {
      initData(){
        getStatisticsDetail({}).then(response => {
          if (response.code === 200) {
            let _that = this;
            this.lastWeek.forEach((item, index) => {
              console.log(index + ":" + item)
            })
            if (response.appointments.length > 0) {
              response.appointments.forEach(p => {
                _that.appointments[p.catalog][_that.lastWeek.getArrayIndex(p.day)] = parseInt(p.num)
              })
            }
            if (response.patients.length > 0) {
              response.patients.forEach(p => {
                _that.patients["新增会员"][_that.lastWeek.getArrayIndex(p.day)] = parseInt(p.num)
              })
            }
            if (response.pays.length > 0) {
              response.pays.forEach(p => {
                _that.pays[p.catalog][_that.lastWeek.getArrayIndex(p.day)] = new Number(p.num).valueOf()
              })
            }
            if (response.records.length > 0) {
              response.records.forEach(p => {
                _that.records[p.catalog][_that.lastWeek.getArrayIndex(p.day)] = parseInt(p.num)
              })
            }

            this.dataList["appointments"] = this.appointments;
            this.dataList["patients"] = this.patients;
            this.dataList["pays"] = this.pays;
            this.dataList["records"] = this.records;

            lineChartData = this.dataList
            this.handleSetLineChartData("patients")

          }
        })
      },
      handleSetLineChartData(type) {
        this.lineChartData = lineChartData[type]
      }
    }
  }
</script>

<style lang="scss" scoped>
  .dashboard-editor-container {
    padding: 32px;
    background-color: rgb(240, 242, 245);
    position: relative;

    .github-corner {
      position: absolute;
      top: 0px;
      border: 0;
      right: 0;
    }

    .chart-wrapper {
      background: #fff;
      padding: 16px 16px 0;
      margin-bottom: 32px;
    }
  }

  @media (max-width: 1024px) {
    .chart-wrapper {
      padding: 8px;
    }
  }
</style>
